$(function(){
	$("#select_subject").click(function(){
		if($.trim($("#name").val()) == ''){
			alert("组卷名称不能为空");
			return false;
		}
		window.location.href="/tiku/add_test_volume/"+$("#subject_id").val()+"/"+$("#name").val();
	})

	$(".next").click(function(){
		$(this).closest(".add_step").hide().next().show();
		$("#name_text").html($("#name").val());
	})

	var total_fraction = 0;

	//模版总分计算
	$(".template_box input[type=text]").on("change",function(){
		var number_one = $(".template_box input[name='number[1]']").val();
		var fraction_one = $(".template_box input[name='fraction[1]']").val();
		var total_one = number_one * fraction_one;

		var number_two = $(".template_box input[name='number[2]']").val();
		var fraction_two = $(".template_box input[name='fraction[2]']").val();
		var total_two = number_two * fraction_two;

		var number_three = $(".template_box input[name='number[3]']").val();
		var fraction_three = $(".template_box input[name='fraction[3]']").val();
		var total_three = number_three * fraction_three;

		total_fraction = total_one + total_two + total_three;
		if(total_fraction > 200){
			alert("总分不能超过200分");
			return false;
		}
		$(".total i").html(total_fraction)
		/*var number_four = $(".template_box input[name='number[4]']").val();
		var fraction_four = $(".template_box input[name='fraction[4]']").val();
		var total_four = number_four * fraction_four;*/
	})

	//保存模版
	$(".add_template").click(function(){
		if($("#template_id").val() > 0){
			return false;
		}
		if(total_fraction > 200){
			alert("总分不能超过200分");
			return false;
		}
		var number_one = $(".template_box input[name='number[1]']").val();
		var fraction_one = $(".template_box input[name='fraction[1]']").val();

		var number_two = $(".template_box input[name='number[2]']").val();
		var fraction_two = $(".template_box input[name='fraction[2]']").val();

		var number_three = $(".template_box input[name='number[3]']").val();
		var fraction_three = $(".template_box input[name='fraction[3]']").val();

		$.ajax({
			type: "POST",
			url: "/tiku/save_test_volume_template",
			dataType:"json",
			data:{'number[1]':number_one,'number[2]':number_two,'number[3]':number_three,'fraction[1]':fraction_one,'fraction[2]':fraction_two,'fraction[3]':fraction_three,'template_name':$("#template_name").val()},
			success: function(result){
				// layer.close(ii);
				alert(result.msg);
				if(result.error == 0){
					var html = "<option value='"+result.template_id+"' selected='selected'>"+result.template_name+"</option>";
					$("#template_id").append(html);
					load_topic();
				}
			}
		});
	})

	layui.use(['form', 'jquery'], function(){
    	var form = layui.form(),$ = layui.jquery;
    	form.on('select(template_id)', function(data){
    		var template_id = $(data.elem).val();
			if(template_id > 0){
				$("#template_name").hide();
				
				$.ajax({
					type: "POST",
					url: "/tiku/get_template_content",
					dataType:"json",
					data:{'template_id':template_id},
					async:false,
					success: function(result){
						// layer.close(ii);
						if(result.error == 0){
							$(".template_box input[type=text]").attr("disabled", true)
							$.each(result.content,function(i,n){
								$(".template_box input[name='number["+i+"]']").val(n.number)
								$(".template_box input[name='fraction["+i+"]']").val(n.fraction)

							});//topic_list_box
							load_topic();
						}else{
							alert(result.msg);
						}
					}
				});
			}else{
				if(template_id == ''){
					$("#template_name").hide();
				}else{
					$("#template_name").show();
				}
				$(".template_box input[type=text]").attr("disabled", false);

				$(".template_box input[name='number[1]']").val(30);
				$(".template_box input[name='fraction[1]']").val(1);

				$(".template_box input[name='number[2]']").val(20);
				$(".template_box input[name='fraction[2]']").val(2);

				$(".template_box input[name='number[3]']").val(30);
				$(".template_box input[name='fraction[3]']").val(1);
				load_topic();
			}
		});
	});
	//获取模版信息
	/*$("#template_id").change(function(){
		var template_id = $(this).val();
		if(template_id > 0){
			$("#template_name").hide();
			
			$.ajax({
				type: "POST",
				url: "/tiku/get_template_content",
				dataType:"json",
				data:{'template_id':template_id},
				async:false,
				success: function(result){
					// layer.close(ii);
					if(result.error == 0){
						$(".template_box input[type=text]").attr("disabled", true)
						$.each(result.content,function(i,n){
							$(".template_box input[name='number["+i+"]']").val(n.number)
							$(".template_box input[name='fraction["+i+"]']").val(n.fraction)

						});//topic_list_box
						load_topic();
					}else{
						alert(result.msg);
					}
				}
			});
		}else{
			if(template_id == ''){
				$("#template_name").hide();
			}else{
				$("#template_name").show();
			}
			$(".template_box input[type=text]").attr("disabled", false);

			$(".template_box input[name='number[1]']").val(30);
			$(".template_box input[name='fraction[1]']").val(1);

			$(".template_box input[name='number[2]']").val(20);
			$(".template_box input[name='fraction[2]']").val(2);

			$(".template_box input[name='number[3]']").val(30);
			$(".template_box input[name='fraction[3]']").val(1);
			load_topic();
		}
	})*/

	function add_datika(){
		if($("input[name='create_topic_type']:checked").val() == 2 && $("#template_id").val() > 0){
			
		}
	}

	$("input[name='create_topic_type']").change(function(){
		load_topic();
	})
	$("#source_type").change(function(){
		load_topic();
	})
	$("#subject_id").change(function(){
		load_topic();
	})

	function load_topic(){
		if($("input[name='create_topic_type']:checked").val() == 2 && $("#template_id").val() > 0){
			$("#topic_list").show();
			
			if($(".chapter_id").length > 0){
				var chapter_id = '';
				$(".chapter_id").each(function(i){
			   		chapter_id += $(this).val();
			   		if(i < $(".chapter_id").length - 1){
			   			chapter_id += '.';
			   		}
				});
			}else{
				var chapter_id = 0;
			}
			var src = "/tiku/get_list/"+$("#source_type").val()+"/"+$("#subject_id").val()+"/"+chapter_id;
			var type_element = $("#topic_list_box").contents().find("#type");
			var subject_text_element = $("#topic_list_box").contents().find("#subject_text");
			if(type_element.length > 0){
				src += "/" + type_element.val();
			}
			if(subject_text_element.length > 0){
				src += "/" + subject_text_element.val();
			}
			$("#topic_list_box").attr('src', src);

			$("#type_box_1").html('');
			$("#type_box_2").html('');
			$("#type_box_3").html('');
			$("#type_box_4").html('');
			var type_box_1 = parseInt($("input[name='number[1]']").val()) + 1;
			var type_box_2 = parseInt($("input[name='number[2]']").val()) + 1;
			var type_box_3 = parseInt($("input[name='number[3]']").val()) + 1;
			var type_box_4 = parseInt($("input[name='number[4]']").val()) + 1;
			var html1 = '';
			for (var i = 1; i < type_box_1; i++) {
				html1 += '<span class="type_1 type_span">'+i+'</span> ';
			}
			$("#type_box_1").html(html1);

			var html2 = '';
			for (var i = 1; i < type_box_2; i++) {
				html2 += '<span class="type_2 type_span">'+i+'</span> ';
			}
			$("#type_box_2").html(html2);

			var html3 = '';
			for (var i = 1; i < type_box_3; i++) {
				html3 += '<span class="type_3 type_span">'+i+'</span> ';
			}
			$("#type_box_3").html(html3);

			var html4 = '';
			for (var i = 1; i < type_box_4; i++) {
				html4 += '<span class="type_4 type_span">'+i+'</span> ';
			}
			$("#type_box_4").html(html4);

		}else{
			$("#topic_list").hide();
			$("#topic_list span").removeClass("selected");
			$("input[type=hidden].selected_topic").remove();
			$("#topic_list_box").attr('src', '');
		}
	}

	

	$('.act-close').on('click', function(){
		$('.acc-data-selector').hide();
		if($(".chapter_id").length > 0){
			$("#selected_chapter").show();
		}else{
			$("#selected_chapter").hide();
		}
	});

	$("#chapter_id").click(function(){
		$(".acc-data-selector").show();
		$(".acc-data-selector-table").show();
	})
	$(".acc-data-selector-selected").click(function(){
		$(".acc-data-selector-table").show();
	})
	$(".ico-check").click(function(){
		var parent = $(this).parent();
		var topic_id = parent.attr("topic_id");
		if(parent.hasClass("checked")){
			parent.removeClass("checked");
			$(".chapter_"+topic_id).remove();
			$("input[name='chapter_id["+topic_id+"]']").remove();
		}else{
			parent.addClass("checked");
			var chapter_name = parent.text().substr(0, parent.text().indexOf(" "));
			var ul = parent.parent().find('li:first');
			var subject_name = ul.text().substr(0, ul.text().indexOf(" "));
			var select_chapter_name = subject_name + ' ' + chapter_name;
			var element_html = '<span class="item chapter_'+topic_id+'" style="display: inline-block;"><span title="'+select_chapter_name+'">'+select_chapter_name+'</span><i class="iclose act-checkItem">×</i></span>';
			element_html += '<input type="hidden" class="chapter_id" name="chapter_id['+topic_id+']" value="'+topic_id+'">';
			parent.closest(".acc-data-selector").find(".acc-data-selector-selected").append(element_html)

			var element_html = '<span style="padding-right:20px;" class="item chapter_'+topic_id+'" style="display: inline-block;"><span title="'+select_chapter_name+'">'+select_chapter_name+'</span></span>';
			$("#selected_chapter").append(element_html)
		}
	})

	$('body').bind('click', function(e){
		var ev = $(e.target);
		var chapter_id = '#chapter_id';
		var chaptext = '.chaptext';
		var iclose = '.iclose,.ico-check';
		var atable = '.acc-data-selector-table'
		var alist = '.acc-data-selector-list';
		var aselected = '.acc-data-selector-selected';
		if(!ev.is(''+chaptext+','+chapter_id+','+iclose+','+chaptext+' span,'+aselected+','+aselected+' span,'+atable+','+atable+' ul,'+atable+' ul li,'+alist+','+alist+' ul,'+alist+' ul li,'+alist+' ul li span')){
			$('.acc-data-selector').hide();
			if($(".chapter_id").length > 0){
				$("#selected_chapter").show()
			}else{
				$("#selected_chapter").hide();
			}
		}
	});

	$(".act-showItems").click(function(){
		var _this = $(this);
		var index = _this.index();
		if(_this.hasClass("active") == false){
			$(".act-showItems").removeClass("active")
			_this.addClass("active")
			$(".acc-data-selector-list").hide();
			$(".acc-data-selector-list").eq(index).show();
		}
	})

	$("form").submit(function(e){
		if($("#template_id").val() <= 0){
			if($("#template_id").val() == ''){
				alert("请选择组卷模版");
			}else{
				alert("请先保存组卷模版");
			}
			return false;
		}
		if($(".selected_topic").length <= 0 && $("input[name='create_topic_type']:checked").val() == 2){
			alert("请选择题目");
			return false;
		}
	});
})